package com.toKnow.service.impl;

import com.toKnow.mapper.*;
import com.toKnow.model.entity.*;
import com.toKnow.service.ArticlePubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("articlePubService")
public class ArticlePubServiceImpl implements ArticlePubService {

    @Autowired
    private ArticlePubDao articlePubDao;
    @Autowired
    private ArticleTypePubDao   articleTypePubDao;
    @Autowired
    private ArticleTagPubDao articleTagPubDao;
    @Autowired
    private UserInfoPubDao userInfoPubDao;
    @Autowired
    private ArticleColumnPubDao articleColumnPubDao;
    @Autowired
    private ArticleNumPubDao articleNumPubDao;
    @Autowired
    private ColumnPubDao columnPubDao;
    @Autowired
    private ArticleCommentAnswerPubDao articleCommentAnswerPubDao;
    @Autowired
    private ArticleCommentPubDao articleCommentPubDao;
    @Autowired
    private ArticleLikePubDao articleLikePubDao;
    @Autowired
    private ArticleCommentLikePubDao articleCommentLikePubDao;
    @Autowired
    private ArticleAnswerLikePubDao articleAnswerLikePubDao;



    public void setArticlePubDao(ArticlePubDao articlePubDao) {
        this.articlePubDao = articlePubDao;
    }

    public void setArticleTypePubDao(ArticleTypePubDao articleTypePubDao) {
        this.articleTypePubDao = articleTypePubDao;
    }

    public void setArticleTagPubDao(ArticleTagPubDao articleTagPubDao) {
        this.articleTagPubDao = articleTagPubDao;
    }

    public void setUserInfoPubDao(UserInfoPubDao userInfoPubDao) {
        this.userInfoPubDao = userInfoPubDao;
    }

    public void setArticleColumnPubDao(ArticleColumnPubDao articleColumnPubDao) {
        this.articleColumnPubDao = articleColumnPubDao;
    }

    public void setArticleNumPubDao(ArticleNumPubDao articleNumPubDao) {
        this.articleNumPubDao = articleNumPubDao;
    }

    public void setColumnPubDao(ColumnPubDao columnPubDao) {
        this.columnPubDao = columnPubDao;
    }

    public void setArticleCommentAnswerPubDao(ArticleCommentAnswerPubDao articleCommentAnswerPubDao) {
        this.articleCommentAnswerPubDao = articleCommentAnswerPubDao;
    }

    public void setArticleCommentPubDao(ArticleCommentPubDao articleCommentPubDao) {
        this.articleCommentPubDao = articleCommentPubDao;
    }

    public void setArticleLikePubDao(ArticleLikePubDao articleLikePubDao) {
        this.articleLikePubDao = articleLikePubDao;
    }

    public void setArticleCommentLikePubDao(ArticleCommentLikePubDao articleCommentLikePubDao) {
        this.articleCommentLikePubDao = articleCommentLikePubDao;
    }

    public void setArticleAnswerLikePubDao(ArticleAnswerLikePubDao articleAnswerLikePubDao) {
        this.articleAnswerLikePubDao = articleAnswerLikePubDao;
    }

    @Override
    @Transactional
    public Integer addArticle(ArticlePub articlePub,  UserInfoPub userInfoPub) {

        articlePubDao.insertArticleReturnId(articlePub);

        userInfoPubDao.updateArticleNum(userInfoPub);

        for(Integer tagId : articlePub.getTagIds()) {
            articleTagPubDao.insertArticleTag(new ArticleTagPub(articlePub.getId(), tagId));
        }

        for(Integer typeId : articlePub.getTypeIds()) {
            articleTypePubDao.insertArticleType(new ArticleTypePub(articlePub.getId(), typeId));
        }

        return articlePub.getId();

    }

    @Override
    @Transactional
    public Integer storeArticle(ArticlePub articlePub,  UserInfoPub userInfoPub) {

        articlePubDao.insertArticleDraftReturnId(articlePub);

        for(Integer tagId : articlePub.getTagIds()) {
            articleTagPubDao.insertArticleDraftTag(new ArticleTagPub(articlePub.getId(), tagId));
        }

        for(Integer typeId : articlePub.getTypeIds()) {
            articleTypePubDao.insertArticleDraftType(new ArticleTypePub(articlePub.getId(), typeId));
        }

        return articlePub.getId();

    }


    @Override
    @Transactional
    public Integer updateDraft(Integer draftId, ArticlePub articlePub,  UserInfoPub userInfoPub) {
        articleTagPubDao.deleteArticleDraftTag(draftId);

        articleTypePubDao.deleteArticleDraftType(draftId);

        articlePub.setId(draftId);

        articlePubDao.updateArticleDraft(articlePub);

        for(Integer tagId : articlePub.getTagIds()) {
            articleTagPubDao.insertArticleDraftTag(new ArticleTagPub(articlePub.getId(), tagId));
        }

        for(Integer typeId : articlePub.getTypeIds()) {
            articleTypePubDao.insertArticleDraftType(new ArticleTypePub(articlePub.getId(), typeId));
        }

        return articlePub.getId();
    }

    @Override
    @Transactional
    public int addArticleTag(ArticleTagPub articleTagPub) {

        int i = articleTagPubDao.insertArticleTag(articleTagPub);

        return i;
    }

    @Override
    @Transactional
    public int storeArticleTag(ArticleTagPub articleTagPub) {

        int i = articleTagPubDao.insertArticleDraftTag(articleTagPub);

        return i;
    }

    @Override
    @Transactional
    public int addArticleType(ArticleTypePub articleTypePub) {

        int i = articleTypePubDao.insertArticleType(articleTypePub);
        return i ;
    }

    @Override
    @Transactional
    public int storeArticleType(ArticleTypePub articleTypePub) {

        int i = articleTypePubDao.insertArticleDraftType(articleTypePub);
        return i ;
    }

    @Override
    @Transactional
    public int moveArticleToColumn(Integer article_id, Integer column_id) {

        //System.out.println("service");

        ArticleNumPub articleNumPub = new ArticleNumPub();
        articleNumPub = articleNumPubDao.selectNumById(article_id);

        ColumnPub columnPub = new ColumnPub();
        columnPub = columnPubDao.selectColumnById(column_id);
        columnPub.setCollect_number( columnPub.getCollect_number() + articleNumPub.getCollect_number());
        columnPub.setRead_number( columnPub.getRead_number() + articleNumPub.getRead_number());
        columnPub.setLike_number( columnPub.getLike_number() + articleNumPub.getLike_number() );
        columnPub.setComment_number(columnPub.getComment_number() + articleNumPub.getComment_number());

        int row1 = columnPubDao.updateColumn(columnPub);

        ArticleColumnPub articleColumnPub = new ArticleColumnPub();
        articleColumnPub.setArticle_id(article_id);
        articleColumnPub.setColumn_id(column_id);
        int row2 = articleColumnPubDao.insertToColumn(articleColumnPub);


        return (row1&row2);

    }


    @Override
    @Transactional
    public int answerArticleComment(ArticleCommentAnswerPub articleCommentAnswerPub) {

        int row1 = articleCommentAnswerPubDao.insertArticleAnswer(articleCommentAnswerPub);

        return row1;
    }

    @Override
    @Transactional
    public int commentArticle(ArticleCommentPub articleCommentPub, Integer article_id, UserInfoPub userInfoPub) {

        //System.out.println("service");

        int row1 = articleCommentPubDao.insertArticleComment(articleCommentPub);
        //System.out.println("row1 = " + row1);

        int row2 = articlePubDao.addArticleComment(article_id);
        //System.out.println("row2 = " + row2);

        List<Integer> columns = articleColumnPubDao.selectColumnOfArticle(article_id);

        for(Integer id : columns){
            columnPubDao.addColumnComment(id);
        }

        int row3 = userInfoPubDao.updateCommentTimes(userInfoPub);
        //System.out.println("row3 = " + row3);

        return (row1&row2&row3);
    }


    @Override
    @Transactional
    public int likeArticle(ArticleLikePub articleLikePub, Integer article_id, Integer author_id) {
        //System.out.println("service");

        Integer count = articleLikePubDao.countLike(articleLikePub);

        if(count == 0){
            int row1 = articleLikePubDao.insertArticleLike(articleLikePub);

            int row2 = articlePubDao.addArticleLike(article_id);

            List<Integer> columns = articleColumnPubDao.selectColumnOfArticle(article_id);

            for(Integer id : columns){
                columnPubDao.addColumnLike(id);
            }

            int row3 = userInfoPubDao.updateLikeTimes(author_id);

            return 1;
        }else {
            int row1 = articleLikePubDao.deleteArticleLike(articleLikePub);

            int row2 = articlePubDao.subArticleLike(article_id);

            List<Integer> columns = articleColumnPubDao.selectColumnOfArticle(article_id);

            for(Integer id : columns){
                columnPubDao.subColumnLike(id);
            }

            int row3 = userInfoPubDao.subLikeTimes(author_id);

            return 2;
        }


    }

    @Override
    @Transactional
    public int likeComment(ArticleCommentLikePub articleCommentLikePub, Integer comment_id) {

        //System.out.println("service");

        Integer count = articleCommentLikePubDao.LikeCount(articleCommentLikePub);

        if ( count == 0 ){

            int row1 = articleCommentLikePubDao.insertCommentLike(articleCommentLikePub);

            int row2 = articleCommentPubDao.addCommentLike(comment_id);

            return 1;

        }else{


            int row1 = articleCommentLikePubDao.deleteCommentLike(articleCommentLikePub);

            int row2 = articleCommentPubDao.subCommentLike(comment_id);

            return 2;


        }




    }

    @Override
    @Transactional
    public int likeAnswer(ArticleAnswerLikePub articleAnswerLikePub, Integer answer_id) {


        Integer count = articleAnswerLikePubDao.countLike(articleAnswerLikePub);

        if(count == 0){

            int row1 = articleAnswerLikePubDao.insertAnswerLike(articleAnswerLikePub);

            int row2 = articleCommentAnswerPubDao.addAnswerLike(answer_id);

            return 1;

        }else {

            int row1 = articleAnswerLikePubDao.deleteAnswerLike(articleAnswerLikePub);

            int row2 = articleCommentAnswerPubDao.subAnswerLike(answer_id);

            return 2;
        }



    }

    @Override
    public Integer findAuthor(Integer article_id) {

        Integer author_id = articlePubDao.selectAuthor(article_id);

        return author_id;
    }
}
